package com.gemo.demo.controller;

import com.gemo.demo.entiy.News;
import com.gemo.demo.service.INewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
import java.util.UUID;

@Controller
public class UploadController {
    @Autowired
    private INewsService newsService;
    @Value("${imgfile}")
    private String targetPath;
    @RequestMapping("/upload")
    public void upload(MultipartFile file,String title,String detail,int tid,String tname) throws Exception{
        System.out.println("文件上传");
        System.out.println(file);
        String fileName="";
        //获取后缀名
        fileName=file.getOriginalFilename();//包含上传文件的后缀名
        String suffix=fileName.substring(fileName.lastIndexOf("."));

        //文件重命名
        fileName= UUID.randomUUID().toString().replace("-","").toLowerCase()+suffix;
        //上传
        file.transferTo(new File(targetPath+"/"+tid,fileName));
        newsService.add(new News(0,title,fileName,detail,new Date(),tid,tname,0));
        return;
    }
}
